package bean.park.map;

import com.alibaba.fastjson.JSON;
import util.JDBCConnection;

import java.util.ArrayList;
import java.util.List;
import java.sql.ResultSet;

public class ParkMapBean {
    int code;
    String message;
    int total_results;
    List<ParkMapDataItem>beanList;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getTotal_results() {
        return total_results;
    }

    public void setTotal_results(int total_results) {
        this.total_results = total_results;
    }

    public List<ParkMapDataItem> getBeanList() {
        return beanList;
    }

    public void setBeanList(List<ParkMapDataItem> beanList) {
        this.beanList = beanList;
    }

    public String queryParkMap(double longitude, double latitude, double longitude_range, double latitude_range){
        JDBCConnection jdbc = new JDBCConnection();
        code = 0;
        message = "成功";
        String querySQL = "select * from parkinglot where abs("+longitude+
                "-lotlongitude)<"+longitude_range+" and abs("+latitude+"-lotlatitude)<"+latitude_range;
        ResultSet rs = jdbc.executeQuery(querySQL);
        int total = 0;
        try{
            List<ParkMapDataItem> list = new ArrayList<>();
            while (rs.next()){
                ParkMapDataItem dataItem = new ParkMapDataItem();
                dataItem.id = rs.getString("lotid");
                dataItem.status = rs.getString("lotstatus");
                dataItem.name = rs.getString("lotname");
                list.add(dataItem);
                total++;
            }
            setTotal_results(total);
            setBeanList(list);
        }
        catch (Exception e){
            message=e.toString();
            code = 100;
        }
        return JSON.toJSONString(this);
    }

}
